/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from "react";
import { Button, Col, Form, Row, DatePicker, Checkbox, message } from "antd";
import KbBrands from "@/components/Select/Brands";
import Kb_Yzs from "@/components/Select/Yzs";

import moment from "moment/moment";

const { RangePicker } = DatePicker;
const CheckboxGroup = Checkbox.Group;
const FormItem = Form.Item;

const disabledDate = current => {
  return (
    current &&
    current >
      moment()
        .endOf("day")
        .subtract(0, "days")
  );
};
const defaultSelectDate = {
  startDate: moment()
    .subtract(1, "month")
    .startOf("month"),
  endDate: moment()
    .subtract(1, "month")
    .endOf("month"),
};

const formItemLayout = {
  labelCol: {
    xs: { span: 12 },
    sm: { span: 6 },
  },
  wrapperCol: {
    xs: { span: 36 },
    sm: { span: 18 },
  },
};

const time_format = "YYYY-MM-DD HH:mm:ss";

const Create = props => {
  const {
    form: { getFieldDecorator, validateFieldsAndScroll, resetFields },
    POST,
  } = props;

  const onSubmit = () => {
    validateFieldsAndScroll((error, values) => {
      if (!error) {
        const { time, flag, ...rest } = values;
        const [start, end] = time;
        POST({
          type: "order_calculate/createBill",
          payload: {
            ...rest,
            start_date: moment(start).format(time_format),
            end_date: moment(end).format(time_format),
            flag: flag?.length ? "1" : "0",
          },
        }).then(() => {
          message.success("生成对账单成功", 1);
          resetFields();
        });
      }
    });
  };

  return (
    <Form {...formItemLayout}>
      <FormItem label="创建时间">
        {getFieldDecorator(`time`, {
          initialValue: [defaultSelectDate.startDate, defaultSelectDate.endDate],
        })(
          <RangePicker
            showTime={{ format: "HH:mm:ss" }}
            format={time_format}
            disabledDate={disabledDate}
            placeholder={["请输入开始时间", "请输入结束时间"]}
            style={{ width: "80%" }}
          />
        )}
      </FormItem>
      <FormItem label="快递品牌">
        {getFieldDecorator(`brand`, {})(
          <KbBrands style={{ width: "80%" }} requestType="source/getKopBrand" />
        )}
      </FormItem>
      <FormItem label="驿站名称">
        {getFieldDecorator(`inn_name`, {})(
          <Kb_Yzs style={{ width: 140 }} requestType="order_calculate/getSubDaksList" />
        )}
      </FormItem>
      <FormItem label="其他设定">
        {getFieldDecorator(`flag`, {})(<CheckboxGroup options={["过滤没有物流的数据"]} />)}
      </FormItem>
      <Row type="flex">
        <Col push={10}>
          <Button type="primary" onClick={onSubmit}>
            生成对账单
          </Button>
        </Col>
      </Row>
    </Form>
  );
};

export default Form.create()(Create);
